<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('AlohacartOrderPrinterExcel')) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

include_spip('inc/filtres');
 /** 
 *  This class provides methods to create list of orders in Excel format
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
class AlohacartOrdersExcel {
    
    /**
	 * AlohacartOrderModel
	 */
	protected $order_mdl;
    
    /**
     * Status of downloaded orders 
     */
    public $OrderIdStatut = false;
    
    public $OrderDateFrom = false;
    public $OrderDateTo = false;
	
	public function __construct() {
		require_once _DIR_PLUGIN_EXCEL."pear/Spreadsheet/Excel/Writer.php";
        
        $this->order_mdl = pdk_load_class('AlohacartOrderModel', 'classes');
	}
	
    /**
     *  Generates Excel file for list of orders and outputs it to stream
     */
    public function OutputFile() {
        
        // Creating a workbook
        $workbook = new Spreadsheet_Excel_Writer();    
        $workbook->setTempDir(_DIR_CACHE);
        $workbook->setVersion(8);
        
        // sending HTTP headers
        $workbook->send('orders.xls');
    
        // Let's create worksheet:
        $this->addWorksheet($workbook, $id_order);
        
        // Let's send the file
        $workbook->close();
    
        // After file has been sent, just exit:
        exit;
    }
	
	/**
	 *  Generates Excel file for list of orders
	 */
	public function GenerateFile($filename = '') {
		// Creating a workbook
        if  ($filename)
            $workbook = new Spreadsheet_Excel_Writer(_DIR_TMP.$filename);            
        else
            $workbook = new Spreadsheet_Excel_Writer('orders.xls');                
        
        $workbook->setVersion(8);
    
        // Let's create worksheet:
        $this->addWorksheet($workbook, $id_order);
        
        // Let's send the file
        $workbook->close();
	}
    
    /**
     *  Adds Worksheet with orders to given $workbook
     */
    private function addWorksheet(&$workbook) {
        // Retrieve orders
        $where = array();
        if ($this->OrderIdStatut)
            $where[] = $this->order_mdl->main_table.'.id_statut='.$this->OrderIdStatut;
        if ($this->OrderDateFrom)
            $where[] = $this->order_mdl->main_table.".date>='".$this->OrderDateFrom."'";
        if ($this->OrderDateTo)
            $where[] = $this->order_mdl->main_table.".date<='".$this->OrderDateTo."'";
		$orders = $this->order_mdl->select($where, false, $this->order_mdl->main_table.'.date DESC');
        //var_Dump($this->order_mdl->last_query); exit;
        
		// Format for first line
        $format_bold =& $workbook->addFormat();
        $format_bold->setBold();        
		
        // Creating worksheet    
        $worksheet =& $workbook->addWorksheet('Order');
        $worksheet->setInputEncoding('utf-8');
        
        // Write first line
        $worksheet->write(0, 0, 'ID', $format_bold);
        $worksheet->write(0, 1, _T('alohacart:client_name'), $format_bold);
        $worksheet->write(0, 2, _T('alohacart:order_status'), $format_bold);
        $worksheet->write(0, 3, _T('alohacart:order_date'), $format_bold);
        $worksheet->write(0, 4, _T('alohacart:order_total_price'), $format_bold);
        
        // Set columns width:
        $worksheet->setColumn(0,0,5); 
        $worksheet->setColumn(1,3,20);
        $worksheet->setColumn(4,4, 15);
		
		$i = 1;
        
        // List of orders
        foreach ($orders as $order) {
            $worksheet->write($i, 0, $order['id_order']);
            $worksheet->write($i, 1, $order['name']);
            $worksheet->write($i, 2, extraire_multi($order['statut_titre']));
            $worksheet->write($i, 3, affdate($order['date'],'d.m.Y, H:i'));
            $worksheet->write($i, 4, $order['total_price']);
            $i++;
        }
    }
}
?>